import java.util.ArrayList;

public class Equations
{
	public Equations()
	{
		
	}
	
	
	/**
	 * calculates the IBL value of each server in the arraylist and sets its utilization to that value.
	 * 
	 * @param servers arraylist of servers to calculate the IBL.
	 * @return arraylist with the same servers after updating the utilization of each server to be equal to its corresponding IBL value.
	 */
	public ArrayList<Double> IBLEquation(ArrayList<Server> servers){
		
		ArrayList<Double> IBL = new ArrayList<Double>();
		
		ArrayList<Double> CPUUtil = new ArrayList<Double>();
		double weightedCPU = 0.0;
		double totalCPU = 0.0;
		
		ArrayList<Double> MemUtil = new ArrayList<Double>();
		double weightedMem = 0.0;
		double totalMem = 0.0;
		
		ArrayList<Double> BWUtil = new ArrayList<Double>();
		double weightedBW = 0.0;
		double totalBW = 0.0;
		
		for(int i = 0; i < servers.size(); i++){
			
			CPUUtil.add(i, servers.get(i).getCPUUtilization());
			weightedCPU += (CPUUtil.get(i) * (servers.get(i).CPU));
			totalCPU += servers.get(i).CPU;
			
			MemUtil.add(i, servers.get(i).getMemUtilization());
			weightedMem += (MemUtil.get(i) * (servers.get(i).Mem));
			totalMem += servers.get(i).Mem;
			
			BWUtil.add(i, servers.get(i).getBWUtilization());
			weightedBW += (BWUtil.get(i) * (servers.get(i).BW));
			totalBW += servers.get(i).BW;
			
		}
		
		double averageCPU = weightedCPU/totalCPU;
		double averageMem = weightedMem/totalMem;
		double averageBW = weightedBW/totalBW;
		
		for(int i = 0; i < servers.size(); i++){
			
			double avg = (CPUUtil.get(i) + MemUtil.get(i) + BWUtil.get(i)) / 3;
			double x = Math.pow((avg - averageCPU), 2);
			double y = Math.pow((avg - averageMem), 2);
			double z = Math.pow((avg - averageBW), 2);
			
			IBL.add(i, (x+y+z)/3);
			servers.get(i).utilization = (x+y+z)/3;
			
		}

		return(IBL);
		
	}
	
}